/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"2E02D34F-868C-4748-8483-8E4B4A1054D6",variableType:4}
 */
var $historico = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"4C95B1FA-4915-4158-93AD-5564971D59F5",variableType:4}
 */
var $historico_listado_roles = 1;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"1C1ACB7D-3E1C-4473-B443-FCE052613C7C",variableType:4}
 */
var $historico_listado_usuarios = 2;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"4BD1F9C8-6255-4F2B-A60D-D7C676BE4C1F",variableType:4}
 */
var $historico_listado_menus_por_perfil = 3;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"778D4353-1B69-4A76-89F0-29CD152E55CA",variableType:4}
 */
var $historico_listado_menus_por_perfil_arbolado = 4;

/**
 * @param {JSEvent} event
 *
 * @properties={typeid:24,uuid:"C1AE6C6A-465D-41DA-99DF-5BC27D3A075F"}
 */
function opciones(event){
	
	var menu = plugins.window.createPopupMenu();
	menu.addMenuItem("Generar Roles", generar_roles);

	if (event.getSource()) {
		menu.show(event.getSource());
	}
}

/**
 * @properties={typeid:24,uuid:"FFFC1716-84D9-4CFD-965B-6FD20D03137E"}
 */
function generar_roles(){
	
	scopes.adm_t900_roles.generarRoles(foundset.getSelectedRecord());
}

/**
 * Hide btn reset fields
 * @author Roberto J. Blasco
 * @version 1.0
 * @private 
 * @properties={typeid:24,uuid:"2FDB19D0-7BE8-414B-8747-1D23E9FB98AA"}
 */
function hide_btn_reset_fields(){
	
	elements.btn_ir_empresa.enabled = true;
	elements.btn_ir_aplicacion.enabled = true;
}

/**
 * doEdit
 * @author Roberto J. Blasco
 * @version 1.0
 * @private 
 * @properties={typeid:24,uuid:"1B3506A6-5F95-4A43-98D0-9400ABADDA61"}
 */
function doEdit(){
	
	if (adm_t900_empresas_id != null){
		globals.hide_btn_reset_fields_elment(controller.getName(),elements.vlst_adm_t900_empresas_id.getName());
	}
	if (adm_t900_aplicaciones_id != null){
		globals.hide_btn_reset_fields_elment(controller.getName(),elements.vlst_adm_t900_aplicaciones_id.getName());
	}
	
	elements.btn_ir_empresa.enabled = false;
	elements.btn_ir_aplicacion.enabled = false;
	controller.focusField(elements.fld_descripcion.getName(),true);
}

/**
 * Callback method for when form is shown.
 * @author Roberto J. Blasco
 * @version 1.0
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 * @private
 * @properties={typeid:24,uuid:"FB2DD360-F864-4682-BCDE-DAFB17447BDA"}
 */
function onShow(firstShow, event) {

	if (foundset.getSelectedRecord().isNew() && adm_t900_perfiles_to_adm_t900_empresas.s_nombre_corto != null){
		s_descripcion = adm_t900_perfiles_to_adm_t900_empresas.s_nombre_corto + "@";
	}else{
		
		var dataset = databaseManager.createEmptyDataSet(0,new Array('display_values','optional_real_values'));
		dataset.addRow(['LISTADO DE ROLES', $historico_listado_roles]);
		dataset.addRow(['LISTADO DE USUARIOS', $historico_listado_usuarios]);
		dataset.addRow(['LISTADO DE MENÚS POR PERFIL', $historico_listado_menus_por_perfil]);
		dataset.addRow(['LISTADO DE MENÚS POR PERFIL ARBOLADO', $historico_listado_menus_por_perfil_arbolado]);
		elements.historicos.setValueListItems(dataset);
		
		elements.tab_historicos.visible = true;
		elements.tab_historicos.removeAllTabs();
		elements.tab_historicos.addTab(forms.lst_adm_t900_roles_h,null,null,null,null,null,null,adm_t900_perfiles_to_adm_t900_roles);
		$historico = $historico_listado_roles;
	}
}

/**
 * Callback method when form is (re)loaded.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"46B42BDE-E9FE-4CBF-9ACA-9D007441EDD6"}
 * @AllowToRunInFind
 */
function onLoad(event) {

	
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @private
 *
 * @properties={typeid:24,uuid:"006C0A63-5DD4-4D00-9854-53994B09311A"}
 */
function onDataChangeHistorico(oldValue, newValue, event) {

	elements.tab_historicos.removeAllTabs();
	if ($historico == $historico_listado_roles){
		elements.tab_historicos.addTab(forms.lst_adm_t900_roles_h,null,null,null,null,null,null,adm_t900_perfiles_to_adm_t900_roles);
	}else if ($historico == $historico_listado_usuarios){
		elements.tab_historicos.addTab(forms.lst_adm_t900_usuarios_h,null,null,null,null,null,null, adm_t900_perfiles_to_adm_t900_usuarios);
	}else if ($historico == $historico_listado_menus_por_perfil){
		elements.tab_historicos.addTab(forms.lst_adm_t300_perfiles_menus__menus_h, null,null,null,null,null,null, adm_t900_perfiles_to_adm_t300_perfiles_menus);
	}else if ($historico == $historico_listado_menus_por_perfil_arbolado){
		elements.tab_historicos.addTab(forms.lst_adm_t900_menus_arbolado_h, null,null,null,null,null,null, null);
	}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"FAFD286B-2C1E-453E-A9B6-047FF67F946B"}
 */
function ir_empresa(event) {
	
	globals.ir_formulario(event, forms.frm_adm_t900_empresas.controller.getName(),adm_t900_perfiles_to_adm_t900_empresas,globals.$MODO_LECTURA);
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"C6CA0D81-63B3-4508-8DFE-8F71D1DE5941"}
 */
function opciones_historico(event) {
	
	var menu = plugins.window.createPopupMenu();
	if ($historico == $historico_listado_roles){
	}else if ($historico == $historico_listado_usuarios){
	}else if ($historico == $historico_listado_menus_por_perfil){
		var submenu_menus_por_perfil_recalcular_menus = menu.addMenuItem("Recalcular Menús", menus_por_perfil_recalcular_menus);
		submenu_menus_por_perfil_recalcular_menus.setIcon("media:///iconos/ic_autorenew_grey600_18dp.png");
	}
	
	if (event.getSource()) {
		menu.show(event.getSource());
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"3FFB805D-AC2C-4018-BE87-2D7AFA67FAAE"}
 */
function alta_historico(event) {
	
	/** @type {JSDataSet} */
	var ds = databaseManager.createEmptyDataSet();
	ds.addRow(['adm_t900_perfiles_id', adm_t900_perfiles_id]);
	
	if ($historico == $historico_listado_menus_por_perfil){
		globals.ir_formulario(event, forms.frm_adm_t300_perfiles_menus.controller.getName(), null, globals.$MODO_ALTA, ds);
	}else if ($historico == $historico_listado_usuarios){
		globals.ir_formulario(event, forms.frm_adm_t900_usuarios.controller.getName(), null,  globals.$MODO_ALTA, ds);
	}else if($historico == $historico_listado_roles){
		globals.ir_formulario(event, forms.frm_adm_t900_roles.controller.getName(),null, globals.$MODO_ALTA, ds);
	}
}

/**
 * @properties={typeid:24,uuid:"C101532D-CA32-4ED1-BFD0-60A97F75256E"}
 */
function menus_por_perfil_recalcular_menus(){
	
	foundset.getSelectedRecord().adm_t900_perfiles_to_adm_t300_perfiles_menus.deleteAllRecords();
	/** @type {JSFoundSet<db:/pigeon_board/adm_t900_menus>} */
	var fs_adm_t900_menus = databaseManager.getFoundSet(scopes.adm_t900_menus.$DATA_SOURCE);
	fs_adm_t900_menus.loadAllRecords();
	/** @type {JSFoundSet<db:/pigeon_board/adm_t300_perfiles_menus>} */
	var fs_adm_t300_perfiles_menus = databaseManager.getFoundSet(scopes.adm_t300_perfiles_menus.$DATA_SOURCE);
	for (var x = 1; x <= databaseManager.getFoundSetCount(fs_adm_t900_menus); x++){
		fs_adm_t300_perfiles_menus.newRecord();
		fs_adm_t300_perfiles_menus.adm_t900_perfiles_id = foundset.getSelectedRecord().adm_t900_perfiles_id;
		fs_adm_t300_perfiles_menus.adm_t900_menus_id = fs_adm_t900_menus.getRecord(x).adm_t900_menus_id;
	}
	databaseManager.saveData(fs_adm_t900_menus);
	
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"BECDA57C-9E88-4D9D-84E0-641A951D079A"}
 */
function ir_aplicacion(event) {

	globals.ir_formulario(event,forms.frm_adm_t900_aplicaciones.controller.getName(),adm_t900_perfiles_to_adm_t900_aplicaciones,globals.$MODO_LECTURA);
}

/**
 * Handle changed data.
 *
 * @param {String} oldValue old value
 * @param {String} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @private
 *
 * @properties={typeid:24,uuid:"8F7948D1-9CD1-49DE-99CC-0082FBC8C992"}
 */
function onDataChange_adm_t900_empresas_id(oldValue, newValue, event) {
	
	if (adm_t900_perfiles_to_adm_t900_empresas.s_nombre_corto != null){
		s_descripcion = adm_t900_perfiles_to_adm_t900_empresas.s_nombre_corto + "@";
	}
	
	return true
}
